import isPhone from './isPhone';

const getDeviceType = () => {
  const userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // 检测 iPadOS
  if (/iPad/i.test(userAgent)) {
    return 'iPadOS';
  } else if (/iPhone/i.test(userAgent)) {
    // 检测 iOS (iPhones)
    return 'iOS';
  } else if (/android/i.test(userAgent)) {
    // 检测 Android
    return 'Android';
  } else if (/windows/i.test(userAgent)) {
    // 检测 Windows
    return 'Windows';
  } else if (/mac os/i.test(userAgent)) {
    // 检测 MacOS
    return 'MacOS';
  } else if (/linux/i.test(userAgent)) {
    // 检测 Linux
    return 'Linux';
  } else {
    // 如果没有匹配到任何已知的平台
    return 'unknown';
  }
};
const getOSVersion = () => {
  const userAgent = window.navigator.userAgent;
  const platform = window.navigator.platform;
  const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
  const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
  const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
  let os = null;

  if (macosPlatforms.indexOf(platform) !== -1) {
    os = 'Mac OS';
  } else if (iosPlatforms.indexOf(platform) !== -1) {
    os = 'iOS';
  } else if (windowsPlatforms.indexOf(platform) !== -1) {
    os = 'Windows';
  } else if (/Android/.test(userAgent)) {
    os = 'Android';
  } else if (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }
  let version = 'Unknown';
  try {
    if (userAgent) {
      switch (os) {
        case 'Mac OS':
          // Mac OS versions are not directly exposed in the user agent string.
          // More complex parsing would be required to extract it.
          break;
        case 'iOS':
          if (userAgent.match(/OS (\d+)_?(\d+)?/)) {
            version = userAgent.match(/OS (\d+)_?(\d+)?/)[0].replace('OS ', '').replace('_', '.');
          }
          break;
        case 'Windows':
          if (userAgent.indexOf('Windows NT 10.0') > -1) version = '10';
          else if (userAgent.indexOf('Windows NT 6.3') > -1) version = '8.1';
          else if (userAgent.indexOf('Windows NT 6.2') > -1) version = '8';
          else if (userAgent.indexOf('Windows NT 6.1') > -1) version = '7';
          else if (userAgent.indexOf('Windows NT 6.0') > -1) version = 'Vista';
          else if (userAgent.indexOf('Windows NT 5.1') > -1) version = 'XP';
          else if (userAgent.indexOf('Windows NT 5.0') > -1) version = '2000';
          break;
        case 'Android':
          if (userAgent.match(/Android (\d+)\.?(\d+)?/)) {
            version = userAgent.match(/Android (\d+)\.?(\d+)?/)[0].replace('Android ', '');
          }
          break;
        default:
          version = 'Unknown';
      }
    }
  } catch (e) {
    console.error(e);
  }

  return version;
};
const getNetworkType = () => {
  const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
  if (connection) {
    return connection.effectiveType;
  } else {
    return 'unknown';
  }
};

export const getCommonHeadInfo = () => {
  return {
    'X-App-Version': 0,
    'X-Platform': isPhone() ? 'mobile_web' : 'web',
    'X-Device-Type': getDeviceType(),
    'X-App-Channel': 'gitcode-news-fe',
    'X-Network-Type': getNetworkType(),
    'X-OS-Version': getOSVersion(),
    'X-Device-ID': 'unknown'
  };
};
